package com.bbb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbb.common.Code;
import com.bbb.common.Result;
import com.bbb.pojo.User;
import com.bbb.pojo.UserLike;
import com.bbb.pojo.UserWorks;
import com.bbb.pojo.WorksLike;
import com.bbb.service.UserLikeService;
import com.bbb.service.UserService;
import com.bbb.service.UserWorksService;
import com.bbb.service.WorksLikeService;
import com.bbb.common.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping("/user/works")
public class UserWorksController {
    @Autowired
    private UserWorksService service;

    @Autowired
    private WorksLikeService worksLikeService;

    @Autowired
    private UserLikeService userLikeService;

    @Autowired
    private UserService userService;

    @RequestMapping("/unLike")
    @Transactional
    public synchronized Result unLike(@RequestParam("id") Integer id) {

        Integer userId = AuthUtil.getUserId();

        LambdaQueryWrapper<WorksLike> userWorksWrapper = new LambdaQueryWrapper<>();
        userWorksWrapper.eq(WorksLike::getUserId, userId);
        userWorksWrapper.eq(WorksLike::getWorksId, id);
        worksLikeService.remove(userWorksWrapper);

        UserWorks userWorks = service.getById(id);
        userWorks.setLikesCount(userWorks.getLikesCount() - 1);
        service.updateById(userWorks);
        return new Result(Code.GET_OK, "操作成功");
    }

    @RequestMapping("/like")
    @Transactional
    public synchronized Result like(@RequestParam("id") Integer id) {

        Integer userId = AuthUtil.getUserId();

        LambdaQueryWrapper<WorksLike> userWorksWrapper = new LambdaQueryWrapper<>();
        userWorksWrapper.eq(WorksLike::getUserId, userId);
        userWorksWrapper.eq(WorksLike::getWorksId, id);

        if (worksLikeService.count(userWorksWrapper) > 0) {
            return new Result(Code.GET_OK, "操作成功");
        }

        WorksLike like = new WorksLike();
        like.setUserId(userId);
        like.setWorksId(id);
        worksLikeService.save(like);

        UserWorks userWorks = service.getById(id);
        userWorks.setLikesCount(userWorks.getLikesCount() + 1);
        service.updateById(userWorks);
        return new Result(Code.GET_OK, "操作成功");
    }

//    @RequestMapping("/self/list")
//    public Result selfLst() {
//        Integer userId = AuthUtil.getUserId();
//
//        List<UserWorks> list = service.list(new LambdaQueryWrapper<UserWorks>().eq(UserWorks::getUserId, userId));
//
//        for (UserWorks userWorks : list) {
//            User user = userService.getById(userWorks.getUserId());
//            userWorks.setAuthorName(Optional.ofNullable(user).map(User::getName).orElse(""));
//
//            LambdaQueryWrapper<WorksLike> userWorksWrapper = new LambdaQueryWrapper<>();
//            userWorksWrapper.eq(WorksLike::getUserId, userId);
//            userWorksWrapper.eq(WorksLike::getWorksId, userWorks.getId());
//            userWorks.setLikeWork(worksLikeService.count(userWorksWrapper) > 0 ? 1 : 0);
//
//            if (Objects.equals(userWorks.getUserId(), userId)) {
//                userWorks.setLikeAuthor(2);
//            }else {
//                LambdaQueryWrapper<UserLike> userLikeWrapper = new LambdaQueryWrapper<>();
//                userLikeWrapper.eq(UserLike::getUserId, userId);
//                userLikeWrapper.eq(UserLike::getAuthorId, userWorks.getUserId());
//                userWorks.setLikeAuthor(userLikeService.count(userLikeWrapper) > 0 ? 1 : 0);
//            }
//        }
//
//        return new Result(Code.GET_OK, "查询成功", list);
//    }

    @RequestMapping("/selfId/list")
    public Result selfLst(@RequestParam(required = false) Integer userId) {
//        System.out.println("得到"+userId);
        if (userId == null) {
            userId = AuthUtil.getUserId();
        }
        List<UserWorks> list = service.list(new LambdaQueryWrapper<UserWorks>().eq(UserWorks::getUserId, userId));

        for (UserWorks userWorks : list) {
            User user = userService.getById(userWorks.getUserId());
            userWorks.setAuthorName(Optional.ofNullable(user).map(User::getName).orElse(""));

            LambdaQueryWrapper<WorksLike> userWorksWrapper = new LambdaQueryWrapper<>();
            userWorksWrapper.eq(WorksLike::getUserId, userId);
            userWorksWrapper.eq(WorksLike::getWorksId, userWorks.getId());
            userWorks.setLikeWork(worksLikeService.count(userWorksWrapper) > 0 ? 1 : 0);

            if (Objects.equals(userWorks.getUserId(), userId)) {
                userWorks.setLikeAuthor(2);
            } else {
                LambdaQueryWrapper<UserLike> userLikeWrapper = new LambdaQueryWrapper<>();
                userLikeWrapper.eq(UserLike::getUserId, userId);
                userLikeWrapper.eq(UserLike::getAuthorId, userWorks.getUserId());
                userWorks.setLikeAuthor(userLikeService.count(userLikeWrapper) > 0 ? 1 : 0);
            }
        }

        return new Result(Code.GET_OK, "查询成功", list);
    }


    @RequestMapping("/list")
    public Result list() {
        List<UserWorks> list = service.list();
        Integer userId = AuthUtil.getUserId();

        for (UserWorks userWorks : list) {
            User user = userService.getById(userWorks.getUserId());
            userWorks.setAuthorName(Optional.ofNullable(user).map(User::getName).orElse(""));

            LambdaQueryWrapper<WorksLike> userWorksWrapper = new LambdaQueryWrapper<>();
            userWorksWrapper.eq(WorksLike::getUserId, userId);
            userWorksWrapper.eq(WorksLike::getWorksId, userWorks.getId());
            userWorks.setLikeWork(worksLikeService.count(userWorksWrapper) > 0 ? 1 : 0);

            if (Objects.equals(userWorks.getUserId(), userId)) {
                userWorks.setLikeAuthor(2);
            }else {
                LambdaQueryWrapper<UserLike> userLikeWrapper = new LambdaQueryWrapper<>();
                userLikeWrapper.eq(UserLike::getUserId, userId);
                userLikeWrapper.eq(UserLike::getAuthorId, userWorks.getUserId());
                userWorks.setLikeAuthor(userLikeService.count(userLikeWrapper) > 0 ? 1 : 0);
            }
        }

        return new Result(Code.GET_OK, "查询成功", list);
    }

    @RequestMapping("/save")
    public Result save(@RequestBody UserWorks userWorks) {
        userWorks.setCreatedAt(new Date());
        userWorks.setLikesCount(0);
        userWorks.setUserId(AuthUtil.getUserId());
        service.save(userWorks);
        return new Result(Code.GET_OK, "新增成功");
    }

    // 新增删除接口
    @DeleteMapping("/delete/{id}")
    @Transactional
    public Result delete(@PathVariable("id") Integer id) {
        // 先删除作品的点赞记录
        LambdaQueryWrapper<WorksLike> worksLikeWrapper = new LambdaQueryWrapper<>();
        worksLikeWrapper.eq(WorksLike::getWorksId, id);
        worksLikeService.remove(worksLikeWrapper);

        // 再删除作品本身
        boolean isDeleted = service.removeById(id);
        if (isDeleted) {
            return new Result(Code.DELETE_OK, "删除成功");
        } else {
            return new Result(Code.DELETE_ERR, "删除失败");
        }
    }

}
